<?php
declare (strict_types = 1);

namespace app\command;

use app\data\logic\AdminUserLogic;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Admin extends Command
{


    protected function configure()
    {
        // 指令配置
        $this->setName('index')
            ->addArgument('type',Argument::REQUIRED,"操作类型，add:添加，del:删除")
            ->addArgument('admin_email',Argument::OPTIONAL,"邮箱账号")
            ->addArgument('admin_password',Argument::OPTIONAL,"邮箱密码")
            ->addOption('del_email',null,Option::VALUE_OPTIONAL,'删除邮箱地址')
            ->setDescription('用户管理');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
      $ret = '';
      switch (trim($input->getArgument('type'))) {
          case 'add':
            $email = trim($input->getArgument('admin_email'));
            $password = trim($input->getArgument('admin_password'));
              $ret = $this->add($email,$password);
              break;

          case 'del':
            $del_email = trim($input->hasOption('del_email') ? $input->getOption('del_email') : '');
              $ret = $this->del($del_email);
              break;
      }
        $output->writeln($ret);
    }

    protected function add($email,$password): string
    {
        $adminUserLogic = new AdminUserLogic();
        // 判断邮箱地址是否正确
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
          return '邮箱地址不正确';
        }
        $data['user_name'] = $email;
        $data['password'] = $password;
        $data['email'] = $email;
        $data['agency_id'] = 1;
        $result = $adminUserLogic->createNewUser($data);
        if($result['state']){
          return '新增用户成功';
        }else{
          return '新增用户失败';
        }
    }

    protected function del($email): string
    {
        $adminUserLogic = new AdminUserLogic();
      // 判断邮箱地址是否正确
      if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        return '邮箱地址不正确';
      }
      $result = $adminUserLogic->delUserByEmail($email);
      if($result['state']){
        return '删除成功';
      }else{
        return '删除失败';
      }
    }
}
